#include <QtUrlEditorFactory.h>
#include <QFileDialog>
#include <EDesignerCore.h>
#include <ModeControlDialog.h>
UrlEditWidget::UrlEditWidget(QWidget *parent, Qt::WindowFlags f):QWidget(parent, f)
{
    m_Hboxlayout = new QHBoxLayout;
    this->setLayout(m_Hboxlayout);
    m_lineEdit = new QLineEdit;
    m_pushButton = new QPushButton;
    m_pushButton->setText("...");
    m_Hboxlayout->addWidget(m_lineEdit);
    m_Hboxlayout->addWidget(m_pushButton);

    m_Hboxlayout->setContentsMargins(0, 0, 0, 0);
    m_Hboxlayout->setSpacing(1);

    QObject::connect(m_pushButton, SIGNAL(clicked()), this, SLOT(SlotButtonClicked()));
}

UrlEditWidget::~UrlEditWidget()
{

}

void UrlEditWidget::setValue(const QUrl &text)
{
    m_lineEdit->setText(text.toString());
}

QUrl UrlEditWidget::getLineText()
{
    QUrl url(m_lineEdit->text());

    return url;
}

void UrlEditWidget::SlotButtonClicked()
{
    QString file_name;
    //file_name = QFileDialog::getOpenFileName(0, QString::fromStdString(std::string("选择文件")), "",  "");

    file_name = EDesignerCore::instance()->modeControlDialog()->enterSelectResourceMode();

    if (file_name.size() != 0 && file_name != m_lineEdit->text())
    {
        m_lineEdit->setText(file_name);
        QUrl url(file_name);
        emit signalUrlStrChanged(url);
    }
}

QtUrlEditorFactory::QtUrlEditorFactory(QObject *parent) : QtAbstractEditorFactory<QtUrlEditorManager>(parent)
{

}

QtUrlEditorFactory::~QtUrlEditorFactory()
{

}


void QtUrlEditorFactory::connectPropertyManager(QtUrlEditorManager *manager)
{
    //connect(manager, SIGNAL(valueChanged(QtProperty*,double)),this, SLOT(slotPropertyChanged(QtProperty*,double)));
}

QWidget *QtUrlEditorFactory::createEditor(QtUrlEditorManager *manager, QtProperty *property, QWidget *parent)
{
    UrlEditWidget* url_edit_widget = new UrlEditWidget(parent);
    url_edit_widget->setValue( manager->value(property));
    m_propertyToWidget[property] = url_edit_widget;
    m_WidgetToProperty[url_edit_widget] = property;
    connect(url_edit_widget, SIGNAL(signalUrlStrChanged(const QUrl&)),this, SLOT(slotSetValue(const QUrl &)));
    return url_edit_widget;
}

void QtUrlEditorFactory::disconnectPropertyManager(QtUrlEditorManager *manager)
{

}

void QtUrlEditorFactory::slotSetValue(const QUrl &url_str)
{
    QObject* sender = QObject::sender();
    UrlEditWidget* url_edit_widget = qobject_cast<UrlEditWidget*>(sender);
    if(url_edit_widget != NULL)
    {
        QtProperty *property = m_WidgetToProperty[url_edit_widget];
        QtUrlEditorManager* manager = propertyManager(property);
        manager->setValue(property, url_str);
    }
}
#include "moc_QtUrlEditorFactory.cxx"
